#define _CRT_SECURE_NO_WARNINGS 1
#include "mystruct.h"

MyStack* myStackCreate() {
    MyStack* obj = (MyStack*)malloc(sizeof(MyStack));
    QueueInit(&(obj->a));
    QueueInit(&(obj->b));
    return obj;
}

void myStackPush(MyStack* obj, int x) {

    if (QueueEmpty(&(obj->a)) == 0)
    {
        QueuePush(&(obj->a), x);
    }
    else
    {
        QueuePush(&(obj->b), x);
    }
}

int myStackPop(MyStack* obj) {
    Queue* empty = &(obj->a);
    Queue* noempty = &(obj->b);
    if (QueueEmpty(noempty) == 1)
    {
        empty = &(obj->b);
        noempty = &(obj->a);
    }
    while (QueueSize(noempty) > 1)
    {
        QueuePush(empty, QueueFront(noempty));
        QueuePop(noempty);
    }
    QDataType kuda = QueueFront(noempty);
    QueuePop(noempty);
    return kuda;
}

int myStackTop(MyStack* obj) {
    Queue* empty = &(obj->a);
    Queue* noempty = &(obj->b);
    if (QueueEmpty(noempty) == 1)
    {
        empty = &(obj->b);
        noempty = &(obj->a);
    }
    return QueueBack(noempty);
}

bool myStackEmpty(MyStack* obj) {
    if ((QueueEmpty(&(obj->a)) == 1) && (QueueEmpty(&(obj->b)) == 1))
    {
        return true;
    }
    else
    {
        return false;
    }
}

void myStackFree(MyStack* obj) {
    QueueDestroy(&(obj->a));
    QueueDestroy(&(obj->b));
    free(obj);
}
